package com.tensequare.spit.controller;

import com.tensequare.spit.pojo.Spit;
import com.tensequare.spit.service.SpitService;
import common.entity.PageResult;
import common.entity.Result;
import common.entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: Feng.Wang
 * @Company: Zelin.ShenZhen
 * @Description:
 * @Date: Create in 2019/3/25 16:15
 */
@RestController
@CrossOrigin
@RequestMapping("/spit")
public class SpitController {
    @Autowired
    private SpitService spitService;

    /**
     * 查询所有的吐槽
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    public Result findAll(){
        return new Result(true, StatusCode.OK,"查询成功",spitService.findAll());
    }

    /**
     * 根据id查询吐槽
     * @return
     */
    @RequestMapping(method = RequestMethod.GET,value = "/{spitId}")
    public Result findById(@PathVariable  String spitId){
        return new Result(true, StatusCode.OK,"根据id查询成功",
                spitService.findById(spitId));
    }
    /**
     * 添加
     * @param spit
     * @return
     */
    @RequestMapping(method = RequestMethod.POST)
    public Result add(@RequestBody  Spit spit){
        //添加到mongodb中
        spitService.add(spit);
        return new Result(true,StatusCode.OK,"添加成功！");
    }
    /**
     * 修改
     * @param spit
     * @return
     */
    @RequestMapping(method = RequestMethod.PUT,value = "/{spitId}")
    public Result update(@RequestBody  Spit spit,@PathVariable String spitId){
        //修改mongodb中
        spit.set_id(spitId);
        spitService.update(spit);
        return new Result(true,StatusCode.OK,"修改成功！");
    }
    /**
     * 根据id删除
     * @param spitId
     * @return
     */
    @RequestMapping(method = RequestMethod.DELETE,value = "/{spitId}")
    public Result deleteById(@PathVariable String spitId){
        //根据id删除
        spitService.deleteById(spitId);
        return new Result(true,StatusCode.OK,"删除成功！");
    }

    /**
     * 根据父id查询吐槽列表
     * @param parentid
     * @return
     */
    @RequestMapping(method = RequestMethod.GET,value = "/comment/{parentid}/{page}/{size}")
    public Result findByParentid(@PathVariable String parentid,@PathVariable int page,@PathVariable int size){
        //获取分页结果
        Page<Spit> pageData = spitService.findByParentid(parentid, page, size);
        return new Result(true,StatusCode.OK,"根据父id查询吐槽列表成功！",
                new PageResult<>(pageData.getTotalElements(),pageData.getContent()));
    }

    /**
     * 吐槽点赞
     * @param spitId
     * @return
     */
    @RequestMapping(value = "/thumbup/{spitId}",method = RequestMethod.PUT)
    public Result updateThumbup(@PathVariable String spitId){
        spitService.updateThumbup(spitId);
        return new Result(true,StatusCode.OK,"点赞成功");
    }
}
